package ch.ffhs.ks.model;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.Date;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.InternetAddress;

public class Mail implements Serializable, Comparable<Mail>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 324922373401120179L;
	private Date sendDate;
	private Date receiveDate;
	private String sender;
	private String receiver;
	private String subject;
	private String content;

	private String cc;
	private String bcc;


	public Mail()
	{

	}

	public Date getSendDate() {
		return sendDate;
	}

	public void setSendDate(Date sendDate) {
		this.sendDate = sendDate;
	}

	public Date getReceiveDate() {
		return receiveDate;
	}

	public void setReceiveDate(Date receiveDate) {
		this.receiveDate = receiveDate;
	}

	public String getSender() {
		return sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public String getReceiver() {
		return receiver;
	}

	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Mail(Message m) throws MessagingException
	{

		sendDate = m.getSentDate();
		receiveDate = m.getReceivedDate();

		sender = joinAddress(m.getFrom());

		receiver = joinAddress(m.getRecipients(RecipientType.TO));
		cc = joinAddress(m.getRecipients(RecipientType.CC));
		bcc = joinAddress(m.getRecipients(RecipientType.BCC));

		subject = m.getSubject();

	}

	public String getCc() {
		return cc;
	}

	public void setCc(String cc) {
		this.cc = cc;
	}

	public String getBcc() {
		return bcc;
	}

	public void setBcc(String bcc) {
		this.bcc = bcc;
	}

	private String joinAddress(Address[] address)
	{
		String result = "";

		if(address != null)
		{
			for(Address a : address)
			{
				result += ((InternetAddress)a).getAddress();
				if(!address[address.length - 1].equals(a))
				{
					result += "; ";
				}
			}
		}

		return result;
	}

	public void LoadContent(Message m) throws IOException, MessagingException
	{

		if(m.getContent() instanceof String)
		{
			this.content = m.getContent().toString();
		}else if(m.getContent() instanceof Multipart)
		{
			this.content = "";

			Multipart multipart = (Multipart) m.getContent();
			//int multipartCount = multipart.getCount();

			for (int b = 0; b < 1; b++) {

				InputStream is = multipart.getBodyPart(b).getInputStream();
				if (!(is instanceof BufferedInputStream)) {
					is = new BufferedInputStream(is);
				}

				int c;
				final StringWriter sw = new StringWriter();
				while ((c = is.read()) != -1) {
					sw.write(c);
				}

				this.content += sw.toString();

			}

		}


	}

	public Object[] getTableData() {
		Object values[] = new Object[]{getValue(getSender()), getValue(getSubject()), getValue(getSendDate())};
		return values;
	}

	@SuppressWarnings("deprecation")
	private String getValue(Date v)
	{
		if(v == null)
		{
			return "-";
		}else
		{
			return v.toLocaleString();
		}
	}

	private String getValue(String v)
	{
		if(v == null)
		{
			return "";
		}else
			return v;
	}

	@Override
	public boolean equals(Object arg0) {

		if(arg0 instanceof Mail)
		{
			Mail secMail = (Mail) arg0;

			return secureEquals(getSender(), secMail.getSender()) &&
					secureEquals(getSendDate(),secMail.getSendDate()) &&
					secureEquals(getReceiveDate(),secMail.getReceiveDate()) &&
					secureEquals(getSubject(), secMail.getSubject());
		}

		return super.equals(arg0);
	}

	public boolean secureEquals(Object firstItem, Object secondItem)
	{
		if(firstItem == null && secondItem == null)
		{
			return true;
		}else if(firstItem == null && secondItem != null)
		{
			return false;
		}else if(firstItem != null && secondItem == null)
		{
			return false;
		}else{
			return firstItem.equals(secondItem);
		}

	}

	@Override
	public int compareTo(Mail o) {

		return o.getSendDate().compareTo(getSendDate());

	}

}
